﻿using GitLabApiClient;
using GitLabApiClient.Models.Branches.Responses;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;

// gitlab api文档https://gitlab.cn/docs/jh/api/projects.html
partial class Program
{
	static async Task<string> GetGitProjectById(int id) {
		var client = new GitLabClient("http://git.oa.com");
		await client.LoginAsync("guoxudong", "Guoxd#com@");
		var p = await client.Projects.GetAsync(id);
		return p.WebUrl;
	}
	static async Task ExportGitBranch(IServiceProvider serviceProvider)
	{
		var log = serviceProvider.GetRequiredService<ILogger<Program>>();
		try
		{
			using (var fs = new FileStream(@"d:\gitBranch.xlsx", FileMode.Create, FileAccess.Write))
			{
				var workbook = new XSSFWorkbook();
				var style1 = workbook.CreateCellStyle();
				var font1 = workbook.CreateFont();
				font1.IsBold = true;
				style1.SetFont(font1);
				style1.Alignment = HorizontalAlignment.Center;

				var style2 = workbook.CreateCellStyle();
				style2.FillForegroundColor = HSSFColor.SkyBlue.Index;
				style2.FillPattern = FillPattern.SolidForeground;
				//style2.FillBackgroundColor = HSSFColor.Red.Index;
				var font2 = workbook.CreateFont();
				font2.IsItalic = true;
				style2.SetFont(font2);

				var style3 = workbook.CreateCellStyle();
				var font3 = workbook.CreateFont();
				font3.Color = HSSFColor.Red.Index;
				style3.SetFont(font3);

				var sheet = workbook.CreateSheet();
				sheet.SetColumnWidth(0, 60 * 256);
				sheet.SetColumnWidth(1, 20 * 256);
				sheet.SetColumnWidth(2, 10 * 256);
				var rowIndex = 0;
				var headRow = sheet.CreateRow(rowIndex++);
				string[] heads = ["分支名", "作者", "已合并"];
				var colIndex = 0;
				foreach (var item in heads)
				{
					var cell = headRow.CreateCell(colIndex++);
					cell.SetCellValue(item);
					cell.CellStyle = style1;
				}

				var client = new GitLabClient("http://git.oa.com");
				await client.LoginAsync("guoxudong", "Guoxd#com@");
				var list = await client.Projects.GetAsync();

				foreach (var project in list.OrderBy(m => m.WebUrl))
				{
					if (project.WebUrl.Contains("hardware") || project.WebUrl.Contains("migrated") || project.WebUrl.Contains("algorithm"))
					{
						continue;
					}
					var branches = await client.Connection.GetPagedListAsync<Branch>($"projects/{project.Id}/repository/branches");

					var showed = false;
					foreach (var branch in branches)
					{
						if (new string[] { "master", "main", "develop" }.Contains(branch.Name))
						{
							continue;
						}
						if (!showed)
						{
							log.LogInformation("项目名：{ProName}，Url：{Url}", project.Name, project.WebUrl);
							var pCell = sheet.CreateRow(rowIndex).CreateCell(0);
							pCell.SetCellValue($"{project.Name}——{project.WebUrl}");
							pCell.CellStyle = style2;
							sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, 2));
							rowIndex++;
							showed = true;
						}
						if (branch.Name == project.DefaultBranch)
						{
							log.LogWarning("主分支设置错误");
							continue;
						}
						log.LogInformation("分支名：{Branch}，是否已合并：{Merged}，作者：{Auth}", branch.Name, branch.Merged, branch.Commit.AuthorName);
						var row = sheet.CreateRow(rowIndex++);
						row.CreateCell(0).SetCellValue(branch.Name);
						row.CreateCell(1).SetCellValue(branch.Commit.AuthorName);
						var cell = row.CreateCell(2);
						cell.SetCellValue(branch.Merged);
						if (branch.Merged)
						{
							cell.CellStyle = style3;
						}
					}
				}
				workbook.Write(fs);
			}
		}
		catch (Exception e)
		{

			throw;
		}
	}
}
